package org.example.sudoku;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Writer;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class QRCreator extends Activity {
	
	protected void onCreate (Bundle savedInstanceState) {
		
//		File file = new File("/sdcard/qr-code-bbc.gif");
//		
//		if (!file.exists()) {
//			Log.e(Sudoku.TAG, "Could not read the file");
//			return;
//		} else {
//			Log.d(Sudoku.TAG, "file read!");
//		}
		
		String text = "Teste!";
		BitMatrix matrix;
		Writer writer = new  QRCodeWriter();
		try {
			matrix = writer.encode(text, BarcodeFormat.QR_CODE, 100, 100);
		} catch (WriterException e) {
			Log.e(Sudoku.TAG,"error encoding -> "+e.getMessage());
			return;
		}
		
		Log.d(Sudoku.TAG, "Values of matrix:");
		Log.d(Sudoku.TAG, "Height: "+matrix.height);
		Log.d(Sudoku.TAG, "RowSize: "+matrix.rowSize);
		Log.d(Sudoku.TAG, "Width: "+matrix.width);
		Log.d(Sudoku.TAG, "toString: "+matrix.toString());
		
		
		
		super.onCreate(savedInstanceState);
        setContentView(R.layout.qr_creator);
//		String MyJpgPath = "/sdcard/qr-code-1.jpg"; //UPDATE WITH YOUR OWN JPG FILE
		String MyJpgPath = "/sdcard/newqr.png"; //UPDATE WITH YOUR OWN JPG FILE
		
		
		File fileNew = new File(MyJpgPath);
	
		
        try {
        	fileNew.createNewFile();
        	
        	 BufferedImage image = toBufferedImage(matrix);
        	
			MatrixToImageWriter.writeToFile(matrix, "png", fileNew);
		} catch (FileNotFoundException e) {
			Log.e(Sudoku.TAG, "FileNotFound");
		} catch (IOException e) {
			Log.e(Sudoku.TAG, "IOException");
			e.printStackTrace();
		}
        
        
		ImageView jpgView = (ImageView)findViewById(R.id.image);
        
        
        
        
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        
        
        Bitmap bm = BitmapFactory.decodeFile(MyJpgPath, options);
        jpgView.setImageBitmap(bm); 
        
//		super.onCreate(savedInstanceState);
//		setContentView(R.layout.qr_creator);
	}

}
